package com.finalterm.product.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 商品配置属性
 */
@Slf4j
@Data
@Component
@RefreshScope
@ConfigurationProperties(prefix = "product")
public class ProductConfigProperties {
    
    /**
     * 缓存配置
     */
    private Cache cache = new Cache();
    
    @Data
    public static class Cache {
        /**
         * 缓存时间（秒）
         */
        private Integer time = 60;
    }
    
    @PostConstruct
    public void init() {
        log.info("商品配置初始化完成，缓存时间: {}秒", cache.time);
    }
    
    /**
     * 设置缓存时间，并记录配置变更
     */
    public void setCacheTime(Integer cacheTime) {
        Integer oldTime = this.cache.time;
        this.cache.time = cacheTime;
        log.info("商品缓存时间配置已更新: {}秒 -> {}秒", oldTime, cacheTime);
    }
}
